<?php
/**
 * User MyReservations list Controller
 *
 * @access 	public
 * @author 	Valentin
 * @copyright	ResMania 2009 all rights reserved.
 * @link		http://developer.resmania.com/api
 */
class RM_User_MyReservations_ListController extends RM_User_MyReservations_MainController
{
    protected $formName = 'MyReservations_List';

    function showAction()
    {
        parent::showAction();        
    }    

    function notesJsonAction()
    {
        try {
            $reservation = $this->getReservation($this->_getParam('reservationID'));
        } catch (RM_Exception $e) {
            return array(
                'data' => array(
                    'success' => false,
                    'message' => $e->getMessage()
                )
            );
        }

        $notes = $this->_getParam('notes');
        $reservation->notes = $notes;
        $reservation->save();

        RM_Notifications_Manager::getInstance()->fire('MyReservationsNote', $reservation);
        return array(
            'data' => array(
                'success' => true
            )
        );
    }

    function cancelJsonAction()
    {
        try {
            $reservation = $this->getReservation($this->_getParam('reservationID'));
        } catch (RM_Exception $e) {
            return array(
                'data' => array(
                    'success' => false,
                    'message' => $e->getMessage()
                )
            );
        }

        $module = new RM_Module_MyReservations();

        try {
            $module->cancel($reservation);
        } catch (RM_Exception $e ) {
            return array(
                'data' => array(
                    'success' => false,
                    'massage' => $e->getMessage()
                )
            );
        }

        return array(
            'data' => array(
                'success' => true
            )
        );
    }

    function infoJsonAction()
    {
        try {
            $reservation = $this->getReservation($this->_getParam('reservationID'));
        } catch (RM_Exception $e) {
            return array(
                'data' => array(
                    'success' => false,
                    'message' => $e->getMessage()
                )
            );
        }

        list($reservationInfoJson, ) = $this->_getSummaryReservationInformation($reservation);
        return array(
            'encoded' => true,
            'data' => "{'success' : true, 'info': $reservationInfoJson}"
        );
    }

    function reservationsJsonAction()
    {
        $config = new RM_Config();
        $currencySymbol = $config->getCurrencySymbol();

        $user = $this->getUser();
        $reservationsModel = new RM_Reservations();
        $reservations = $reservationsModel->fetchAllByUserID($user->id);

        $reservationsJson = array();
        foreach ($reservations as $reservation) {
            $details = $reservation->getDetails();
            $totalPrice = $reservation->getTotalPrice();
            $totalPaid = $reservation->getTotalPaid();

            foreach ($details as $detail) {
                $reservationJson = new stdClass();
                $reservationJson->reservation_id = $reservation->id;
                $reservationJson->detail_id = $detail->id;
                $reservationJson->unit_id = $detail->unit_id;
                $reservationJson->unit_name = $detail->findUnit()->name;
                $reservationJson->start_date = $detail->start_datetime;
                $reservationJson->end_date = $detail->end_datetime;
                $reservationJson->price = $currencySymbol.$detail->getTotalPrice();
                $reservationJson->total_price = $currencySymbol.$totalPrice;
                $reservationJson->total_paid = $currencySymbol.$totalPaid;
                $reservationJson->cancel = $reservation->cancel;
                $reservationJson->notes = $reservation->notes;
                $reservationsJson[] = $reservationJson;
            }
        }

        $json = new stdClass();
        $json->data = $reservationsJson;
        $json->total = count($reservationsJson);
        return array(
            'data' => $json
        );
    }

}